package com.itboye.sunsun.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class UpdateRsetPassActivity extends BaseActivity implements
		OnClickListener {

	private ImageView back;
	private EditText newpasswordedit;
	private EditText confirmedit;
	private EditText confirmediqueren;

	private Button changebutton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_passwold);

		setStatusBarColor(R.color.home_blue);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;

		case R.id.changebutton:

			if (confirmedit.getText().equals(
					confirmediqueren.getText().toString())) {
				ByAlert.alert("旧密码错误！");
				System.out.println("两次密码不一致" + confirmedit.getText().toString()
						+ "\n" + confirmediqueren.getText().toString());

			} else {
				changepassword();

			}

		default:
			break;
		}

	}

	private void changepassword() {

		String uid = (String) SPUtils
				.get(App.ctx, null, SPContants.USER_ID, "");

		if (newpasswordedit.getText().equals("")) {
			ByAlert.alert("请输入旧密码");
		} else if (confirmediqueren.getText().equals("")) {
			ByAlert.alert("请输入密码");
		} else if (confirmediqueren.getText().toString().length() < 8) {
			ByAlert.alert("至少输入8位的字符");
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_User_updatePassword")
				.param("uid", uid)
				.param("old_psw", newpasswordedit.getText().toString())
				.param("psw", confirmedit.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						Toast.makeText(App.ctx, response, 0).show();
						finish();
						// login();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							// Toast.makeText(App.ctx, msg, 0).show();
						} else {
							// Toast.makeText(App.ctx, exception.toString(),
							// 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>()
				.apiVer("100").typeKey("BY_User_login")
				.param("password", newpasswordedit.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());

						SPUtils.put(App.ctx, null, SPContants.USERNAME,
								response.getUsername());
						SPUtils.put(App.ctx, null, SPContants.MOBILE,
								response.getMobile());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD,
								newpasswordedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						SPUtils.put(App.ctx, null, SPContants.EMAIL,
								response.getEmail());
						SPUtils.put(App.ctx, null, SPContants.NICK,
								response.getNickname());

						SPUtils.put(App.ctx, null, SPContants.PASSWORD,
								response.getPassword());

						SPUtils.put(App.ctx, null, SPContants.USER_ID,
								response.getUid());
						SPUtils.put(App.ctx, null, SPContants.AVATAR_URL,
								response.getHead());

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());
						UpdateRsetPassActivity.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0)
									.show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
